#!/bin/sh

# This is the BATOCERA system splash service
# It shows the unique BATOCERA logo in early boot stage

LOG_FILE="/var/log/splash.log"

do_start () {
    # on some sytems, fb0 is not immediatly loaded, so, keep a chance by waiting a bit
    # are we sure all boards have fb0 ?!
    LOG_DIR=$(dirname "${LOG_FILE}")
    mkdir -p "${LOG_DIR}"
    exec >> "${LOG_FILE}" 2>&1

    N=0
    while ! [ -e /dev/fb0 ] && [ $N -le 80 ]; do
        sleep 0.25
        N=$((N+1))
    done

    if ! [ -e /dev/fb0 ]; then
        echo "Error: Framebuffer device /dev/fb0 not found." >> $LOG_FILE
        exit 1
    fi

    # Default image
    image="/usr/share/batocera/splash/boot-logo.png"

    if [ -r /sys/class/graphics/fb0/modes ]; then
        MODE_STR=$(head -n 1 /sys/class/graphics/fb0/modes)
        RES_DIM=$(echo "$MODE_STR" | sed -n 's/.*:\([0-9][0-9]*\)x\([0-9][0-9]*\).*/\1 \2/p')
        
        if [ -n "$RES_DIM" ]; then
            set -- $RES_DIM
            WIDTH=$1
            HEIGHT=$2

            echo "Resolution detected via modes: ${WIDTH}x${HEIGHT}" >> $LOG_FILE

            if [ $((WIDTH * 3)) -eq $((HEIGHT * 4)) ]; then
                alt_image="/usr/share/batocera/splash/boot-logo-4x3.png"
                
                if [ -f "$alt_image" ]; then
                    echo "4:3 Aspect Ratio detected. Switching to: $alt_image" >> $LOG_FILE
                    image="$alt_image"
                fi
            fi
        fi
    fi

    if ! [ -f "$image" ]; then
        echo "Error: Splash image not found at ${image}" >> $LOG_FILE
        exit 1
    fi

    FBV_OPTS="-f -e -i -y"
    video_rotation=""
    BOOT_CONF="/boot/batocera-boot.conf"

    if [ -f "${BOOT_CONF}" ]; then
        specific_key=$(grep -oE '^[[:space:]]*display\.rotate\.[^=[:space:]]+' "${BOOT_CONF}" | head -1)

        if [ -n "${specific_key}" ]; then
            video_rotation=$(/usr/bin/batocera-settings-get -f "${BOOT_CONF}" "${specific_key}")
        fi

        if [ -z "${video_rotation}" ]; then
            video_rotation=$(/usr/bin/batocera-settings-get -f "${BOOT_CONF}" "display.rotate")
        fi
    fi

    if [ -z "${video_rotation}" ]; then
        V_BOARD_MODEL=$(batocera-model)
        MODEL_CONF="/usr/share/batocera/sysconfigs/batocera.conf.${V_BOARD_MODEL}"
        if [ -f "${MODEL_CONF}" ]; then
            # Grep for lines starting with "display.rotate." that do not contain a hyphen in the key part
            rotation_line=$(grep -E '^display\.rotate\.[^=-]+=' "${MODEL_CONF}" | head -1)
            if [ -n "${rotation_line}" ]; then
                video_rotation=$(echo "${rotation_line}" | cut -d'=' -f2)
            fi
        fi
    fi

    echo "Final Rotation Value: [${video_rotation}]" >> $LOG_FILE

    case "${video_rotation}" in
        1|2|3)
            FBV_OPTS="${FBV_OPTS} -o ${video_rotation}"
            ;;
    esac

    V_BOARD_MODEL=$(batocera-model)
    MODELOPTS="/etc/opts.$V_BOARD_MODEL"
    if [ -e "${MODELOPTS}" ]; then
        . "${MODELOPTS}"
    fi

    echo "Executing Command: fbv ${FBV_OPTS} \"${image}\"" >> $LOG_FILE

    fbv ${FBV_OPTS} "${image}" >/dev/null 2>&1
}

do_stop () {
    if [ -e /dev/fb0 ]; then
        dd if=/dev/zero of=/dev/fb0 >/dev/null 2>&1
    fi
}

case "$1" in
    start)
        do_start &
        ;;
    stop)
        do_stop
        ;;
    *)
        echo "Usage: $0 {start|stop}"
        exit 1
        ;;
esac

exit $?
